package com.dotools.toutiaolibrary;

import android.app.Activity;
import android.view.View;

import com.bykv.vk.openvk.TTNtExpressObject;
import com.bykv.vk.openvk.TTVfConstant;
import com.bykv.vk.openvk.TTVfDislike;
import com.bykv.vk.openvk.TTVfManager;
import com.bykv.vk.openvk.TTVfNative;
import com.bykv.vk.openvk.VfSlot;

import java.util.List;

import api.ttInteraction.Interaction_API_TT;

/**
 * 模板插屏
 */
public class TT_ExpressInteraction extends Interaction_API_TT {
    private TTVfNative mTTAdNative;
    private TTNtExpressObject ttOb;

    @Override
    public void LoadTTInteraction(final Activity activity, String appid, String adposid, int expressViewWidth, int expressViewHeight, int downloadMode, final TTInteractionListener listener) {
        if (appid != null) {
            if (appid.trim().equals("")) {
                return;
            }
        } else {
            return;
        }
        TTVfManager manager = TTManagerHolder.get();
        if (manager != null) {
            mTTAdNative = manager.createVfNative(activity.getApplicationContext());
            //设置广告参数
            VfSlot vfSlot = new VfSlot.Builder()
                    .setCodeId(adposid) //广告位id
                    .setSupportDeepLink(true)
                    .setAdCount(1) //请求广告数量为1到3条
                    .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight) //期望个性化模板广告view的size,单位dp
                    .build();
            if (downloadMode == 1) {
                vfSlot.setDownloadType(TTVfConstant.DOWNLOAD_TYPE_POPUP);
            }
            //step5:请求广告，对请求回调的广告作渲染处理
            mTTAdNative.loadItExpressVi(vfSlot, new TTVfNative.NtExpressVfListener() {
                @Override
                public void onError(int code, String message) {
                    listener.onError(code, message);
                }

                @Override
                public void onNtExpressVnLoad(List<TTNtExpressObject> ads) {
                    if (ads == null || ads.size() == 0) {
                        return;
                    }
                    listener.onLoad();
                    ttOb = ads.get(0);
                    bindAdListener(activity, ttOb, listener);
                    ttOb.render();
                }
            });
        }else{
            listener.onError(666, "TTVfManager is null  可能原因sdk未初始化成功");
        }
    }

    //绑定广告行为
    private void bindAdListener(final Activity activity, final TTNtExpressObject ad, final TTInteractionListener listener) {
        ad.setExpressInteractionListener(new TTNtExpressObject.NtInteractionListener() {
            @Override
            public void onClicked(View view, int i) {
                listener.onObClicked(i);
            }

            @Override
            public void onShow(View view, int i) {
                listener.onObShow(i);
            }

            @Override
            public void onRenderFail(View view, String s, int i) {
                listener.onRenderFail(s, i);
            }

            @Override
            public void onRenderSuccess(View view, float v, float v1) {
                listener.onRenderSuccess();
                ad.showInteractionExpressAd(activity);
            }

            @Override
            public void onDismiss() {

            }
        });
        bindDislike(activity, ad);
    }


    private void bindDislike(Activity activity, TTNtExpressObject ad) {
        //使用默认模板中默认dislike弹出样式
        ad.setDislikeCallback(activity, new TTVfDislike.DislikeInteractionCallback() {

            @Override
            public void onShow() {

            }

            @Override
            public void onSelected(int i, String s, boolean b) {

            }

            @Override
            public void onCancel() {
            }

        });
    }

    @Override
    public void onDestroy() {
        if (ttOb != null) {
            //调用destroy()方法释放
            ttOb.destroy();
        }
    }

}
